import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-06-07
 * Time: 18:07
 */
public class Main {
    public static void main1(String[] args) throws SQLException {
        //1.获取数据库源
        MysqlDataSource source = new MysqlDataSource();
        source.setURL("jdbc:mysql://127.0.0.1:3306/jdbctest?characterEncoding=utf8&useSSL=false");
        source.setUser("root");
        source.setPassword("111111");
        //2.建立连接
        Connection connection = source.getConnection();
        //3.构建sql语句
        String sql = "select * from student";

        PreparedStatement statement = connection.prepareStatement(sql);

        //4.sql语句发送到服务器上
        ResultSet sets = statement.executeQuery();//查询相关

        while (sets.next()) {
            System.out.println("" + sets.getInt("id") + "  " + sets.getString("name"));
        }

        //5.关闭资源
        statement.close();
        connection.close();
    }

    public static void main(String[] args) throws SQLException {
        //1. 获取数据库源
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://127.0.0.1:3306/jdbctest?characterEncoding=utf8&useSSL=false");
        dataSource.setUser("root");
        dataSource.setPassword("111111");
        //2.建立连接
        Connection connection = dataSource.getConnection();//处理一下异常

        //3.构建sql语句
        String sql = "insert into student values(666,'BOSS')";
        PreparedStatement statement = connection.prepareStatement(sql);


        //4.把sql语句发送到服务器
        int n = statement.executeUpdate();
        System.out.println("改变了" + n + "行");

        //5.关闭资源
        statement.close();
        connection.close();
    }
}
